/*
 * Este programa es software libre; usted puede redistribuirlo y/o modificarlo bajo los terminos
 * de la licencia "GNU General Public License" publicada por la Fundacion "Free Software Foundation".
 * Este programa se distribuye con la esperanza de que pueda ser util, pero SIN NINGUNA GARANTIA;
 * vea la licencia "GNU General Public License" para obtener mas informacion.
 */
package adalid.core;

import adalid.core.interfaces.Entity;
import adalid.core.interfaces.EnumerationEntity;
import adalid.core.interfaces.PersistentEntity;
import adalid.core.interfaces.Property;
import adalid.core.primitives.BinaryPrimitive;
import adalid.core.sql.QueryTable;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;

/**
 * @author Jorge Campins
 */
public class ReportGroup extends AbstractArtifact implements Comparable<ReportGroup> {

    private Report _report;

    private int _sequence;

    private boolean _detail;

    private List<ReportField> _fields;

    public ReportGroup(Report report) {
        if (report != null) {
            _report = report;
            _sequence = Integer.MAX_VALUE;
            _detail = true;
            _fields = new ArrayList<>();
            PersistentEntity entity = report.getEntity();
            if (entity != null) {
                QueryTable queryTable = entity.getQueryTable();
                if (queryTable != null) {
                    Entity e;
                    Property p;
                    ReportField field;
                    List<Property> columns = entity.getDataProviderColumnsList();
                    if (columns != null && !columns.isEmpty()) {
                        for (Property column : columns) {
                            if (column.isHiddenField() || !column.isReportField()) {
                                continue;
                            }
                            if (column instanceof Entity) {
                                e = (Entity) column;
                                p = e.getBusinessKeyProperty();
                                if (p != null && p.isReportField() && queryTable.contains(p)) {
                                    field = new ReportField(this, p);
                                    field.setParentProperty(column);
                                }
                                if (column instanceof EnumerationEntity) {
                                } else {
                                    p = e.getNameProperty();
                                    if (p != null && p.isReportField() && queryTable.contains(p)) {
                                        field = new ReportField(this, p);
                                        field.setParentProperty(column);
                                    }
                                }
                            } else if (column instanceof BinaryPrimitive) {
                            } else if (column instanceof Primitive) {
                                if (column.isReportField()) {
                                    field = new ReportField(this, column);
                                    field.setParentProperty(null);
                                }
                            }
                        }
                    }
                }
            }
            setDeclared("detailGroup");
            add();
        }
    }

    public ReportGroup(Report report, int sequence) {
        _report = report;
        _sequence = sequence;
        _detail = false;
        setDeclared("reportGroup" + sequence);
        add();
    }

    private void add() {
        _report.getGroups().add(this);
    }

    /**
     * @return the report
     */
    public Report getReport() {
        return _report;
    }

    /**
     * @return the sequence
     */
    public int getSequence() {
        return _sequence;
    }

    /**
     * @param sequence the sequence to set
     */
    void setSequence(int sequence) {
        _sequence = sequence;
    }

    /**
     * @return the detail
     */
    public boolean isDetail() {
        return _detail;
    }

    /**
     * @param detail the detail to set
     */
    void setDetail(boolean detail) {
        _detail = detail;
    }

    /**
     * @return the fields list
     */
    public List<ReportField> getFields() {
        return _fields;
    }

    /**
     * @param fields the fields list to set
     */
    void setFields(List<ReportField> fields) {
        _fields = fields;
    }

    // <editor-fold defaultstate="collapsed" desc="Comparable">
    @Override
    public int compareTo(ReportGroup o) {
        ReportGroup that;
        if (o != null) {
            that = o;
            String thisName = StringUtils.trimToEmpty(this.getName());
            String thatName = StringUtils.trimToEmpty(that.getName());
            return thisName.compareTo(thatName);
        }
        return 0;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="toString">
    @Override
    protected String fieldsToString(int n, String key, boolean verbose, boolean fields, boolean maps) {
        String tab = verbose ? StringUtils.repeat(" ", 4) : "";
        String fee = verbose ? StringUtils.repeat(tab, n) : "";
        String faa = " = ";
        String foo = verbose ? EOL : ", ";
        String string = super.fieldsToString(n, key, verbose, fields, maps);
        if (fields || verbose) {
            if (verbose) {
                string += fee + tab + "sequence" + faa + _sequence + foo;
                string += fee + tab + "detail" + faa + _detail + foo;
            }
        }
        return string;
    }
    // </editor-fold>

}
